# -*- coding:utf-8 -*-
from Tkinter import *
from ttk import *


class DeviceSelector:
    def __init__(self, master, adb_manager):
        """构建下拉选择框，选择已连接的设备列表"""
        self.master = Frame(master)
        self.master.pack(side=TOP, fill=X, expand=False)
        self.adb = adb_manager

        Label(self.master, text="选择设备").pack(side=LEFT, anchor=W)
        self.cbb_devices = Combobox(self.master)
        self.cbb_devices["state"] = "readonly"
        self.cbb_devices.bind("<<ComboboxSelected>>", self.select_device)
        self.cbb_devices.pack(side=LEFT, fill=X, expand=True, padx=4)
        self.refresh_devices()
        Button(self.master, text="刷新设备", command=self.refresh_devices).pack(side=RIGHT, anchor=E)

    def refresh_devices(self):
        devices = self.adb.get_devices()
        self.cbb_devices["values"] = devices
        self.cbb_devices.current(0)
        self.adb.update_sn_by_index(0)

    def select_device(self, *args):
        self.adb.update_sn_by_index(self.cbb_devices.current())
